import Vue from 'vue';
import Vuex from 'vuex';
import { passwordLogin, codeLogin } from '@/api/login.js';
import { selectByToken, updateUserInfo, getSchools, getmajors, getProvinces, selectEmployByToken,updateEmployInfo, getJobNames } from "@/api/userInformation";

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    token: uni.getStorageSync('token') || '',
    userInfo: uni.getStorageSync('userInfo') || null,
    role: uni.getStorageSync('role') || 'USER',
    schoolList: [], // 新增学校列表
    majorList: [], // 新增专业列表
    provinceList: [], // 新增地区列表
    jobList: [] ,// 新增职位列表
    showTabBar: true // 控制导航栏显示
  },
  mutations: {
    // 设置token
    SET_TOKEN(state, token) {
      state.token = token;
      uni.setStorageSync('token', token);
    },
    // 设置用户信息
    SET_USER_INFO(state, userInfo) {
      state.userInfo = userInfo;
      uni.setStorageSync('userInfo', userInfo);
    },
    // 设置角色
    SET_ROLE(state, role) {
      state.role = role;
      uni.setStorageSync('role', role);
    },
    // 设置学校列表
    SET_SCHOOL_LIST(state, schoolList) {
      state.schoolList = schoolList;
    },
    // 设置专业列表
    SET_MAJOR_LIST(state, majorList) {
      state.majorList = majorList;
    },
    // 设置地区列表
    SET_PROVINCE_LIST(state, provinceList) {
      state.provinceList = provinceList;
    },
    // 设置职位列表
    SET_JOB_LIST(state, jobList) {
      state.jobList = jobList;
    },
    // 退出登录
    LOGOUT(state) {
      state.token = '';
      state.userInfo = null;
      state.role = 'USER';
      uni.removeStorageSync('token');
      uni.removeStorageSync('userInfo');
      uni.removeStorageSync('role');
    },
    // 设置导航栏显示状态
    SET_SHOW_TAB_BAR(state, show) {
      state.showTabBar = show;
    }
  },
  actions: {
    // 密码登录
    async passwordLogin({ commit }, { email, password, role }) {
      const res = await passwordLogin({ email, password, role });

      if (res.code === 200) {
        const { token, ...userInfo } = res.data;
        commit('SET_TOKEN', token);
        commit('SET_USER_INFO', userInfo);
        commit('SET_ROLE', res.data.role);
        return Promise.resolve(res);
      } else {
        return Promise.reject(res.msg || '登录失败');
      }
    },

    // 验证码登录
    async codeLogin({ commit }, { email, captcha, role }) {
      const res = await codeLogin({ email, captcha, role });

      if (res.code === 200) {
        const { token, ...userInfo } = res.data;
        commit('SET_TOKEN', token);
        commit('SET_USER_INFO', userInfo);
        commit('SET_ROLE', res.data.role);
        return Promise.resolve(res);
      } else {
        return Promise.reject(res.msg || '登录失败');
      }
    },

    // 查询学生用户信息
    async queryUserInfo({ commit }) {
      const res = await selectByToken();
      if (res.code === 200) {
        commit('SET_USER_INFO', res.data);
      }
    },


    // 查询企业用户信息
    async queryEmployInfo({ commit }) {
      const res = await selectEmployByToken();
      if (res.code === 200) {
        commit('SET_USER_INFO', res.data);
      }
    },

    

    // 获取学校列表
    async getSchoolList({ commit }, schoolName) {
      const res = await getSchools(schoolName);
      if (res.code === 200) {
        commit('SET_SCHOOL_LIST', res.data);
      }
    },

    // 获取专业列表
    async getMajorList({ commit }, majorName) {
      const res = await getmajors(majorName);
      if (res.code === 200) {
        commit('SET_MAJOR_LIST', res.data);
      }
    },

    // 获取地区列表
    async getProvinceList({ commit }) {
      const res = await getProvinces();
      if (res.code === 200) {
        commit('SET_PROVINCE_LIST', res.data);
      }
    },

    // 获取职位列表
    async getJobNamesList({ commit }) {
      const res = await getJobNames();
      if (res.code === 200) {
        commit('SET_JOB_LIST', res.data);
      }
    },

    // 退出登录
    logout({ commit }) {
      commit('LOGOUT');
    },

    // 更新学生用户信息
    async updateUserInfo({ commit }, userInfo) {
      const res = await updateUserInfo(userInfo);
      if (res.code === 200) {
        commit('SET_USER_INFO', res.data);
        return Promise.resolve(res);
      } else {
        return Promise.reject(res.msg || '更新失败');
      }
    },

    // 更新企业用户信息
    async updateEmployInfo({ commit }, employInfo) {
      const res = await updateEmployInfo(employInfo);
      if (res.code === 200) {
        commit('SET_USER_INFO', res.data);
        return Promise.resolve(res);
      } else {
        return Promise.reject(res.msg || '更新失败');
      }
    }
  },
  getters: {
    // 判断是否已登录
    isLogin: state => !!state.token,
    // 获取用户信息
    userInfo: state => state.userInfo,
    // 获取角色
    role: state => state.role,
    // 获取学校列表
    schoolList: state => state.schoolList,
    // 获取专业列表
    majorList: state => state.majorList,
    // 获取地区列表
    provinceList: state => state.provinceList,
    // 获取职位列表
    jobList: state => state.jobList
  }
});

export default store;
